<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);

$path = dirname(__FILE__);

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

date_default_timezone_set('Europe/Sofia');

require_once('Zend/Loader/Autoloader.php');

Zend_Loader_Autoloader::getInstance()->registerNamespace('Zend');
Zend_Loader_Autoloader::getInstance()->registerNamespace('Webclub');

try {
	$opts = new Zend_Console_Getopt(
		array(
			'help|h' => 'Display usage information',
			'action|a=s' => 'Action to perform in format of module.controller.action',
			'verbose|v' => 'Verbose messages will be dumped to the default output.',
			'env|e-s' => 'defines application environment (defaults to "production")'
		)
	);
	
	$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
	echo $e->getMessage() . "\n\n" . $e->getUsageMessage();
	
	return false;
}

if (isset($opts->h)) {
	echo $opts->getUsageMessage();
	
	return true;
}

$env = $opts->getOption('e');

// Define application environment
defined('APPLICATION_ENV')
	|| define('APPLICATION_ENV', (null === $env) ? ( getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production') : $env);
	

if (isset($opts->a)) {
	$application = new Zend_Application (
		APPLICATION_ENV,
		APPLICATION_PATH . '/configs/application.ini'
	);

	// bootstrap and retrive the frontController resource
	$front = $application->getBootstrap()
		->bootstrap('frontController')
		->getResource('frontController');

	// magic starts from this line!
	//
	// we will use Zend_Controller_Request_Simple and some kind of custom code
	// to emulate missed in Zend Framework ecosystem
	// "Zend_Controller_Request_Cli" that can be found as proposal here:
	// http://framework.zend.com/wiki/display/ZFPROP/Zend_Controller_Request_Cli
	//
	// I like the idea to define request params separated by slash "/"
	// for ex. "module/controller/action/param1/param2/param3/.."
	//
	// NOTE: according to the current implementation param1,param2,param3,... are omited
	//    only module/controller/action are used
	//
	// TODO: allow to omit "module", "action" params
	//      and set them to "default" and "index" accordantly
	//
	// so lets split the params we've received from the CLI
	// and pass them to the reqquest object
	// NOTE: I think this functionality should be moved to the routing
	$params = array_reverse(explode('/', $opts->getOption('a')));
	$module = array_pop($params);
	$controller = array_pop($params);
	$action = array_pop($params);
    
    $count = count($params);
    $value = false;
    $parameters = array();
    
    for ($i=0; $i < $count; $i++)
    {
        if ($i % 2) {
            $parameters[$params[$i]] = $value;
        } else {
            $value = $params[$i];
        }
    }
    
	$request = new Zend_Controller_Request_Simple ($action, $controller, $module, $parameters);

	// set front controller options to make everything operational from CLI
	$front->setRequest($request)
	   ->setResponse(new Zend_Controller_Response_Cli())
	   ->setRouter(new Webclub_Controller_Router_Cli())
	   ->throwExceptions(true);

	// lets bootstrap our application and enjoy!
	$application->bootstrap()->run();
}